package l1j.opqlo.NewSystem.Extension;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;

public class PrivateRoom {

	class StageRunS extends TimerTask {
		private final int _time = 10000;

		public void begin() {
			final Timer timer = new Timer();
			timer.scheduleAtFixedRate(this, this._time, this._time); // 延遲時間
		}

		@Override
		public void run() {
			try {
				if (PrivateRoom.this._pcList.isEmpty()) {
					PrivateRoom.this._room.cancel();
					PrivateRoom.this._room = null;

				} else {
					// final int Itemid ,final int npcId ,final int count,final
					// int X,final int Y,final short mapId,final int
					// randomRange,final int timeMillisToDelete ,final boolean
					// isBOSS) {

					L1PcInstance pc;
					final int pc_size = PrivateRoom.this._pcList.size();
					for (int i = 0; i < pc_size; i++) {
						pc = PrivateRoom.this._pcList.get(i);
						if (pc.isDead()
								|| pc.getMapId() != PrivateRoom.this._mapid) {
							PrivateRoom.this._pcList.remove(pc);
						}
					}
					if (!PrivateRoom.this._mobList.isEmpty()) {
						for (int i = 0; i < PrivateRoom.this._mobList.size(); i++) {
							if (PrivateRoom.this._mobList.get(i).isDead()) {
								PrivateRoom.this._mobList.remove(i);
							}
						}
					}
					final int mob_count = pc_size * 5
							- PrivateRoom.this._mobList.size();
					if (mob_count > 0) {

						L1NpcInstance[] mobList = TreasureMaster.getInstance()
								.spawn(0, PrivateRoom.this._mobid, mob_count,
										0, 0, (short) PrivateRoom.this._mapid,
										1, 0, false);

						for (final L1NpcInstance element : mobList) {
							PrivateRoom.this._mobList.add(element);
						}
						mobList = null;
					}
					// _mobList.add();
				}
			} catch (final Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}
		}
	}

	private static final Logger _log = Logger.getLogger(PrivateRoom.class
			.getName());
	private final ArrayList<L1PcInstance> _pcList;
	// private L1PcInstance _pc;
	private final int _mobid;
	private StageRunS _room;
	private final int _mapid = 9800;

	private final ArrayList<L1NpcInstance> _mobList = new ArrayList<L1NpcInstance>();
	// private L1NpcInstance[] _mob;
	private static final int Mob = 2000238;

	private static final int[] _loc = { 32743, 32795, 9800 };

	public PrivateRoom(final L1PcInstance pc, final int type) {
		this._pcList = new ArrayList<L1PcInstance>();
		this._pcList.add(pc);
		this._mobid = Mob;
		L1Teleport.teleport(pc, _loc[0], _loc[1], (short) _loc[2], 0, true);
	}

	public PrivateRoom(final L1PcInstance[] pc, final int type) {
		this._pcList = new ArrayList<L1PcInstance>();
		for (final L1PcInstance element : pc) {
			this._pcList.add(element);
			L1Teleport.teleport(element, _loc[0], _loc[1], (short) _loc[2], 0,
					true);
		}
		this._mobid = Mob;
	}

	public void begin() {

		this._room = new StageRunS();
		this._room.begin();

	}

}
